package com.hsck.order.kds.setup.fragment.dyjgl

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hjq.toast.Toaster
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentAddPrinterBinding
import com.hsck.order.kds.setup.SetupActivity
import kotlinx.android.synthetic.main.common_head_black.*

/**
 * 添加修改打印机
 */
class AddPrinterFragment : BaseFragment<FragmentAddPrinterBinding>(),
    SetupActivity.UpdatableFragment {

    var type = ""
    var topFragment: Fragment? = null

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentAddPrinterBinding {
        return FragmentAddPrinterBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        if (type == "Add") {
            tv_head_black_title.text = "添加打印机"
        } else {
            tv_head_black_title.text = "修改打印机"
            binding.btnAddPrinterDel.visibility = View.VISIBLE
        }

        //获取打印机品牌界面传值
        LiveDataBus.with<ArrayList<CategoryBean>>("PrinterBrandFragment").observe(this) {
            var name = ""
            it.forEach {
                name = if (name == "") {
                    it.tagName
                } else {
                    "$name,${it.tagName}"
                }
            }
            binding.tvAddPrinterPinpai.text = "$name"
        }

        binding.apply {
            //USB
            linAddPrinterUsb.setOnClickListener {
                cbAddPrinterUsb.isChecked = true
                cbAddPrinterNetWork.isChecked = false
                cbAddPrinterBluetooth.isChecked = false
                linAddPrinterSn.visibility = View.VISIBLE
                linAddPrinterIp.visibility = View.GONE
                linAddPrinterMac.visibility = View.GONE
            }
            //网口
            linAddPrinterNetWork.setOnClickListener {
                cbAddPrinterUsb.isChecked = false
                cbAddPrinterNetWork.isChecked = true
                cbAddPrinterBluetooth.isChecked = false
                linAddPrinterSn.visibility = View.GONE
                linAddPrinterIp.visibility = View.VISIBLE
                linAddPrinterMac.visibility = View.GONE
            }
            //蓝牙
            linAddPrinterBluetooth.setOnClickListener {
                cbAddPrinterUsb.isChecked = false
                cbAddPrinterNetWork.isChecked = false
                cbAddPrinterBluetooth.isChecked = true
                linAddPrinterSn.visibility = View.GONE
                linAddPrinterIp.visibility = View.GONE
                linAddPrinterMac.visibility = View.VISIBLE
            }

            //80MM
            linAddPrinter80.setOnClickListener {
                cbAddPrinter80.isChecked = true
                cbAddPrinter76.isChecked = false
                cbAddPrinter58.isChecked = false
            }
            //76MM
            linAddPrinter76.setOnClickListener {
                cbAddPrinter80.isChecked = false
                cbAddPrinter76.isChecked = true
                cbAddPrinter58.isChecked = false
            }
            //58MM
            linAddPrinter58.setOnClickListener {
                cbAddPrinter80.isChecked = false
                cbAddPrinter76.isChecked = false
                cbAddPrinter58.isChecked = true
            }
            //选择品牌
            tvAddPrinterPinpai.setOnClickListener {
                var printerBrandFragment = PrinterBrandFragment()//关联档口
                val activity = requireActivity() as SetupActivity
                printerBrandFragment.type = "Edit"
                printerBrandFragment.topFragment = this@AddPrinterFragment
                activity.switchFragment(printerBrandFragment)
            }
            //删除
            btnAddPrinterDel.setOnClickListener {
                setBack()
            }
            //保存
            btnAddPrinterSave.setOnClickListener {
                if (checkInput()) {
                    setBack()
                }
            }
        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

    /**
     * 检查输入数据是否合法
     */
    private fun checkInput(): Boolean {
        when {
            binding.edtAddPrinterName.text.toString().trim() == "" -> {
                Toaster.show("请输入打印机名称")
                return false
            }
            binding.linAddPrinterSn.visibility == View.VISIBLE && binding.edtAddPrinterSn.text.toString()
                .trim() == "" -> {
                //判断USB时候 SN是否为空
                Toaster.show("请输入打印设备编码")
                return false

            }
            binding.linAddPrinterIp.visibility == View.VISIBLE -> {
                //判断网口时候 IP是否为空
                if (binding.edtAddPrinterIp1.text.toString().trim() == "") {
                    Toaster.show("请输入网口IP地址")
                    return false
                }
                if (binding.edtAddPrinterIp2.text.toString().trim() == "") {
                    Toaster.show("请输入网口IP地址")
                    return false
                }
                if (binding.edtAddPrinterIp3.text.toString().trim() == "") {
                    Toaster.show("请输入网口IP地址")
                    return false
                }
                if (binding.edtAddPrinterIp4.text.toString().trim() == "") {
                    Toaster.show("请输入网口IP地址")
                    return false
                }
                return true
            }
            binding.linAddPrinterMac.visibility == View.VISIBLE && binding.edtAddPrinterMac.text.toString()
                .trim() == "" -> {
                //判断蓝牙时候 mac是否为空
                Toaster.show("请输入打印设备蓝牙MAC地址")
                return false

            }
            binding.tvAddPrinterPinpai.text.toString().trim() == "" -> {
                Toaster.show("请选择打印机品牌")
                return false

            }
        }
        return true
    }

}